
import {GetFatModel,UpdateFat} from "../../../../api/api";
// 获取身高
const userheight = []

for (let i = 140; i <= 200; i++) {
  userheight.push(i)
}
const userBirth = []

for (let i = 140; i <= 200; i++) {
  userBirth.push(i)
}



Page({
  data: {
    // 身高
    userheight: userheight,
    heightValue:["20"],
    currentHeightValue:160,
    userHeightValue:"160",
    isShowHeight:false,
    //昵称
    currentNameValue:"",
    userNameValue:"昵称764726",
    isShowName:false,
    //手机号
    currentPhoneValue:"",
    userPhoneValue:"",
    isShowPhone:false,
    // 出生年月
    isShowBirth:false,
    years: [],
    months: [],
    days: [],
    dateIndex:[40,5,5],
    year: "1990",
    month: "6",
    day: "6",
    birthValue:[40,5,5],
    // 性别
    currentSex:0,
    userSex:0,
    isShowSex:false,
    myTel:"",
    //用户id
    userid:""
  },

  // 身高
  // 改变身高
  bindChangeHeight: function (e) {
    const val = e.detail.value
    this.setData({
      currentHeightValue: val[0]+140,
    })
  },
  // 显示身高弹框
  showHeight(){
    this.setData({
      isShowHeight:true,
      heightValue:[this.data.userHeightValue-140]
    })

  },
  hiddenHeight(){
    this.setData({
      isShowHeight:false
    })
  },
  saveHeight(){
    let {currentHeightValue}=this.data;
    this.setData({
      userHeightValue:currentHeightValue
    })
    this.saveAllData();
    this.hiddenHeight();
  },

  // 昵称
    showName(){
      this.setData({
        isShowName:true,
      })
  
    },
    hiddenName(){
      this.setData({
        isShowName:false
      })
    },
    changeName(e){
      const val = e.detail.value
      this.setData({
        currentNameValue:val
      })
    },
    saveName(){
      if(!this.data.currentNameValue){
        wx.showToast({

          title: '昵称不能为空',
     
          icon: 'none',
     
          duration: 2000//持续的时间
     
        })
        return;
      }
      this.setData({
        userNameValue:this.data.currentNameValue
      })
      // 保存数据
      this.saveAllData();
      this.hiddenName();
    },

  // 手机号
    showPhone(){
      this.setData({
        isShowPhone:true,
      })
  
    },
    hiddenPhone(){
      this.setData({
        isShowPhone:false
      })
    },
    changePhone(e){
      const val = e.detail.value
      this.setData({
        currentPhoneValue:val
      })
    },
    savePhone(){
      if(!/^1[3456789]\d{9}$/.test(this.data.currentPhoneValue)){
        wx.showToast({

          title: '请输入正确的手机号',
     
          icon: 'none',
     
          duration: 2000//持续的时间
     
        })
        return;
      }
      this.setData({
        userPhoneValue:this.data.currentPhoneValue
      })
      
      this.saveAllData();
      this.hiddenPhone();
    },

  // 出生年月
  bindChangeBirth: function (e) {
    const val = e.detail.value
    this.setData({
      currentBirthValue: val[0]+140,
    })
  },
  showBirth(){
    this.setData({
      isShowBirth:true,
      BirthValue:[this.data.userBirthValue-140]
    })

  },
  hiddenBirth(){
    this.setData({
      isShowBirth:false
    })
  },
  // 获取当前月份的天数
  mGetDate(year, month){
    var d = new Date(year, month, 0);
    return d.getDate();
  },
  saveBirth(){
    let {dateIndex,years,months,days}=this.data;
    this.setData({
        year:years[dateIndex[0]],
        month:months[dateIndex[1]],
        day:days[dateIndex[2]],
        birthValue:dateIndex
    })
    this.saveAllData();
    this.hiddenBirth();
  },
  changeDate(e){
    const val = e.detail.value;
      this.setData({
        // year:this.data.years[val[0]],
        // month:this.data.months[val[1]],
        // day:this.data.days[val[2]],
        dateIndex:val
      })
      this.getDays(this.data.years[val[0]],this.data.months[val[1]]);
  },

  // 获取天数
  getDays(year,month){
    const days = [];
    for (let i = 1; i <= this.mGetDate(year,month); i++) {
      days.push(i)
    }
    this.setData({
      days
    })
  },

  getDateIndex(){
        //获取对应的日期在日期数组中的索引
        let yearIndex=this.data.years.findIndex((item)=>item==this.data.year);
        let monthIndex=this.data.months.findIndex((item)=>item==this.data.month);
        let dayIndex=this.data.days.findIndex((item)=>item==this.data.day);
        let birthValue=[];
        birthValue[0]=yearIndex;
        birthValue[1]=monthIndex;
        birthValue[2]=dayIndex;
        this.setData({
          birthValue
        })
  },
  //性别
  toggleSex(e){
       let sexIndex=e.currentTarget.dataset.sex;
       this.setData({
        currentSex:sexIndex
       })
  },
  saveSex(){
      this.setData({
        userSex:this.data.currentSex
      })
      this.saveAllData();
      this.hiddenSex();
  },
  showSex(){
    this.setData({
      isShowSex:true,
      currentSex:this.data.userSex
    })

  },
  hiddenSex(){
    this.setData({
      isShowSex:false
    })
  },

  //保存数据
  saveAllData(){
    let {userid,userNameValue,userPhoneValue,userSex,year,month,day,userHeightValue}=this.data;
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    let data={
      id:userid,
      nickname:userNameValue,
      tel: userPhoneValue,
      sex: userSex,
      birthdate:`${year}-${month}-${day}`,
      height:userHeightValue/100,
      weight:'70',
      photo:'/img/ss.png',
    };
      UpdateFat(data,(e)=>{
        wx.hideLoading()
      })
  },
  handleGetUserInfo(e){

  },
  onLoad: function (options) {
    let phone=wx.getStorageSync('TZCphone');
    let openid=wx.getStorageSync('TZCopenid');
    if (!openid) {
      openid=""
    }    

    let getdata={
          phone:phone,
          openid:openid
        }
    if(phone){
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      GetFatModel(getdata,(e)=>{
        wx.hideLoading()
        let ageData=e.data.birthdate.substring(0,10).split("-");
        this.setData({
          userNameValue:e.data.nickname,
          currentNameValue:e.data.nickname,
          userHeightValue:e.data.height * 100||160,
          heightValue:[e.data.height * 100 - 140],
          currentHeightValue:e.data.height * 100||160,
          userid:e.data.id,
          currentSex:e.data.sex,
          userSex:e.data.sex,
          currentPhoneValue:e.data.tel,
          userPhoneValue:e.data.tel,
          year: ageData[0],
          month: ageData[1],
          day: ageData[2],
        })
        this.getDateIndex();
        this.setData({
          dateIndex:this.data.birthValue
        })
        // 获取个人信息
      // userNameValue 昵称
      // userHeightValue 身高
      //     heightValue:["110"],//当前显示的选项
      //     year: "",  出生年月
      //     month: "",
      //     day: "",
      // birthValue 选项框 选项显示
      //   currentSex:0,  //选项框的性别
      //     userSex:0, //性别
      // userPhoneValue:"",
      // userid:""
      })
    }
    //获取出生日期的选项
    const date = new Date()
    const years = []
    const months = []
    let year="2021";
    let month="3";
    let day="19";

    for (let i = 1950; i <= date.getFullYear()-2; i++) {
      years.push(i)
    }

    for (let i = 1; i <= 12; i++) {
      months.push(i)
    }

    this.getDays(year,month);

    this.setData({
      years,
      months,
    })
      



  },

})